﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;

namespace NetTopologySuite.Utilities
{
	public class FunctionsUtil
	{
		public static Envelope GetEnvelopeOrDefault(IGeometry g)
		{
			if (g != null)
			{
				return g.EnvelopeInternal;
			}
			return FunctionsUtil.DefaultEnvelope;
		}

		public static IGeometryFactory GetFactoryOrDefault(IGeometry g)
		{
			if (g != null)
			{
				return g.Factory;
			}
			return FunctionsUtil.Factory;
		}

		public static IGeometryFactory GetFactoryOrDefault(IEnumerable<IGeometry> gs)
		{
			if (gs == null)
			{
				return FunctionsUtil.Factory;
			}
			foreach (IGeometry geometry in gs)
			{
				if (geometry != null)
				{
					return geometry.Factory ?? FunctionsUtil.Factory;
				}
			}
			return FunctionsUtil.Factory;
		}

		public static IGeometry BuildGeometry(List<IGeometry> geoms, IGeometry parentGeom)
		{
			if (geoms.Count <= 0)
			{
				return null;
			}
			if (geoms.Count == 1)
			{
				return geoms[0];
			}
			if (parentGeom != null && parentGeom.OgcGeometryType == OgcGeometryType.GeometryCollection)
			{
				return parentGeom.Factory.CreateGeometryCollection(GeometryFactory.ToGeometryArray(geoms));
			}
			return FunctionsUtil.GetFactoryOrDefault(geoms).BuildGeometry(geoms);
		}

		public static IGeometry BuildGeometry(params IGeometry[] geoms)
		{
			return FunctionsUtil.GetFactoryOrDefault(geoms).CreateGeometryCollection(geoms);
		}

		public static IGeometry BuildGeometry(IGeometry a, IGeometry b)
		{
			int num = 0;
			if (a != null)
			{
				num++;
			}
			if (b != null)
			{
				num++;
			}
			IGeometry[] array = new IGeometry[num];
			num = 0;
			if (a != null)
			{
				array[num++] = a;
			}
			if (b != null)
			{
				array[num] = b;
			}
			return FunctionsUtil.GetFactoryOrDefault(array).CreateGeometryCollection(array);
		}

		public static readonly Envelope DefaultEnvelope = new Envelope(0.0, 100.0, 0.0, 100.0);

		private static readonly IGeometryFactory Factory = new GeometryFactory();
	}
}
